<template>
  <el-radio-group v-model="modelValueProxy" v-bind="$attrs">
    <el-radio v-for="item in options" :key="item.value" :label="item.value">{{ item.label }}</el-radio>
  </el-radio-group>
</template>

<script setup>
import { ref, watch, onMounted } from 'vue'
import { getDictItemsByCode } from '../api/dict'

const props = defineProps({
  type: { type: String, required: true },
  modelValue: [String, Number],
})
const emit = defineEmits(['update:modelValue'])

const options = ref([])
const loading = ref(false)
const modelValueProxy = ref(props.modelValue)

watch(() => props.modelValue, val => {
  modelValueProxy.value = val
})
watch(modelValueProxy, val => {
  emit('update:modelValue', val)
})

function loadOptions() {
  if (!props.type) return
  loading.value = true
  getDictItemsByCode(props.type).then(res => {
    options.value = res.data
  }).finally(() => {
    loading.value = false
  })
}

onMounted(loadOptions)
watch(() => props.type, loadOptions)
</script> 